package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.dto.admin.AdminCategoryRemoveDto;
import com.example.course.dto.admin.AdminCategorySaveDto;
import com.example.course.mapper.AdminMapper;
import com.example.course.pojo.Admin;
import com.example.course.pojo.Category;
import com.example.course.service.CategoryService;
import com.example.course.mapper.CategoryMapper;
import com.example.course.utils.common.enums.Code;
import com.example.course.utils.common.result.PageResponseResult;
import com.example.course.utils.common.result.Result;
import com.example.course.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 24354
* @description 针对表【category】的数据库操作Service实现
* @createDate 2024-03-15 15:14:26
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result saveCategory(AdminCategorySaveDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //业务异常抛出
        Category checkCategory = categoryMapper.selectOne(Wrappers.<Category>lambdaQuery()
                .eq(Category::getCategoryName, dto.getName()));
        if (checkCategory!=null) {
            return Result.errorResult(Code.DATA_EXIST);
        }
        //构建添加
        Category category=new Category();
        category.setCategoryName(dto.getName());
        int checkSave = categoryMapper.insert(category);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeCategory(Long id) {
        //空值抛出
        if (id==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //删除
        int checkRemove = categoryMapper.deleteById(id);
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result getAdminCategory(AdminCategoryRemoveDto dto) {
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //充值页码
        dto.checkParam();
        //构建page实体
        IPage page=new Page(dto.getPage(),dto.getSize());
        //获取数据库数据
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        if (!dto.getMimo().equals("")) {
            wrapper.like(Category::getCategoryName,dto.getMimo());
        }
        //注入组件
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }
}




